﻿using System.IO;
using System.IO.Compression;

namespace Hotel.Alliance.CommonEsLib.Tools
{
    /// <summary>
    /// GZip压缩帮助类
    /// </summary>
    internal static class GZipHelper
    {
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="bytes">输入的byte数组</param>
        /// <returns>压缩后的byte数组</returns>
        public static byte[] Compress(byte[] bytes)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
                {
                    gZipStream.Write(bytes, 0, bytes.Length);
                    gZipStream.Close();

                    return stream.ToArray();
                }
            }
        }

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="bytes">压缩后的byte数组</param>
        /// <returns>解压缩后的byte数组</returns>
        public static byte[] Decompress(byte[] bytes)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (GZipStream gZipStream = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        gZipStream.CopyTo(stream);
                        gZipStream.Close();

                        return stream.ToArray();
                    }
                }
            }
        }
    }
}
